/**
 *
 */
package gemini.basic.dao.impl;

import gemini.basic.dao.GmnDao;
import gemini.basic.dao.IdentityCardDao;
import gemini.basic.model.IdentityCard;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 *
 */
public class IdentityCardDaoImpl extends HibernateDaoSupport implements IdentityCardDao {

	private GmnDao gmnDao;

	public void setGmnDao(GmnDao gmnDao) {
		this.gmnDao = gmnDao;
	}

	@Override
	public IdentityCard saveOrUpdate(IdentityCard identityCard, boolean flush) {
		IdentityCard result = null;
		if (flush) {
			result = (IdentityCard) gmnDao.saveOrUpdateAndFlush(identityCard);
		} else {
			result = (IdentityCard) gmnDao.saveOrUpdate(identityCard);
		}
		return result;
	}
	
	@Override
	public boolean existedIdentity(String number) {

		DetachedCriteria dtCri = DetachedCriteria.forClass(IdentityCard.class);

		if (number != null && !("".equals(number))) {
			dtCri.add(Restrictions.eq("number", number));
		}

		List<Object> disList = gmnDao.findByCriteria(dtCri);
		
		boolean result = (disList != null && !disList.isEmpty());
		return result;
	}

}
